<template>
  <div class="app-container">

    <div style="margin-bottom: 10px;">
      <el-button
        icon="el-icon-download"
        size="mini"
        type="primary"
        @click="dialogVisible = true"
      >
        导入Excel
      </el-button>

      <el-button
        icon="el-icon-upload2"
        size="mini"
        type="primary"
        @click="exportData"
      >
        导出Excel
      </el-button>

    </div>

    <el-dialog
      :visible.sync="dialogVisible"
      title="数据字典导入"
      width="30%"
    >
      <el-form>
        <el-form-item label="请选择Excel文件">
          <el-upload
            :action="BASE_API + '/admin/dict/import'"
            :auto-upload="true"
            :limit="1"
            :multiple="false"
            :on-error="fileUploadError"
            :on-success="fileUploadSuccess"
            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            name="file"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
      </div>
    </el-dialog>

    <el-table :data="list" :load="load" border lazy row-key="id">
      <el-table-column align="left" label="名称" prop="name" />
      <el-table-column label="编码" prop="dictCode" />
      <el-table-column align="left" label="值" prop="value" />
    </el-table>

  </div>
</template>

<script>
import dictApi from '@/api/core/dict'

export default {
  name: 'Index',
  data() {
    return {
      BASE_API: process.env.VUE_APP_BASE_API,//获取后端地址
      dialogVisible: false,
      list: []
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      dictApi.listByParentId(1)
        .then(response => {
          this.list = response.data.list
          console.log(this.list)
        })
        .catch(error => {
          console.log(error)
        })
    },
    fileUploadSuccess(response) {
      if (response.code === 0) {
        this.dialogVisible = false
        this.$message.success('数据字典导入成功')
      } else {
        this.$message.error(response.message)
      }
    },
    fileUploadError() {
      this.$message.error('数据字典导入失败')
    },
    exportData() {
      window.location.href = this.BASE_API + '/admin/dict/export'
    },
    load(tree, treeNode, resolve) {
      dictApi.listByParentId(tree.id)
        .then(response => {
          /* setTimeout(() => {
            resolve(response.data.list)
          }, 1000) */
          resolve(response.data.list)
        })
    }
  }
}
</script>

<style scoped>

</style>
